home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / archiver / ltarv3.zip / LOCATE.C < prev    next >
C/C++ Source or Header  |  1991-09-09  |  2KB  |  113 lines

  1. #include <conio.h>
  2. #include <ctype.h>
  3. #include <dos.h>
  4. #include <dir.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9. #define MAXPAT    256
  10.  
  11. char curDir[MAXPAT], filename[12] ;
  12. char tDrv[3], tDir[MAXPAT], tName[9], tExt[5] ;
  13. char *loc = "*.*", *applyComd ;
  14.  
  15. int halt = 0 ;
  16.  
  17. attachTarget( char *loc ) {
  18.  
  19.     int c ;
  20.  
  21.     c = fnsplit( loc, tDrv, tDir, tName, tExt ) ;
  22.     if ( c & DRIVE ) {
  23.         setdisk( toupper( tDrv[0] ) - 'A' ) ;
  24.         if ( getdisk() != toupper( tDrv[0] ) - 'A' )
  25.             return 0 ;
  26.     }
  27.     if ( c & DIRECTORY )
  28.         if ( chdir( tDir ) ) {
  29.             tDir[strlen( tDir ) - 1] = 0 ;
  30.             if ( chdir( tDir ) )
  31.                 return 0 ;
  32.         }
  33.     if ( c & FILENAME )
  34.         strncpy( filename, tName, 8 ) ;
  35.     if ( c & EXTENSION )
  36.         strncat( filename, tExt, 4 ) ;
  37.     filename[11] = 0 ;
  38.     printf( "LOCATE %s\n", filename ) ;
  39.     return 1 ;
  40. }
  41.  
  42. void applyCommand( char *name ) {
  43.  
  44.     static char command[MAXPAT], *ptr ;
  45.  
  46.     if ( applyComd ) {
  47.         if ( ( ptr = strstr( applyComd, "$s" ) ) != NULL )
  48.             *ptr = '%' ;
  49.         sprintf( command, applyComd, name ) ;
  50.         puts( command ) ;
  51.         system( command ) ;
  52.     }
  53. }
  54.  
  55. char *locatedfile( char *name ) {
  56.  
  57.     static char compTarget[MAXPAT] ;
  58.     static char *str ;
  59.  
  60.     loc = getcwd( tDir, MAXPAT ) ;
  61.     str = strlen( loc ) == 3 ? "%s%s" : "%s\\%s" ;
  62.  
  63.     sprintf( compTarget, str, loc, name ) ;
  64.     return compTarget ;
  65. }
  66.  
  67. void recusiveSearch( void ) {
  68.  
  69.     static struct ffblk tag ;
  70.  
  71.     struct ffblk dir ;
  72.     int c ;
  73.  
  74.     for ( c = findfirst( "*.*", &dir, FA_DIREC | FA_RDONLY | FA_ARCH ) ;
  75.         !c ; c = findnext( &dir ) ) {
  76.         if ( halt )
  77.             break ;
  78.         if ( dir.ff_attrib & FA_DIREC ) {
  79.             loc = dir.ff_name ;
  80.             if ( *loc != '.' ) {
  81.                 chdir( loc ) ;
  82.                 recusiveSearch() ;
  83.             }
  84.         }
  85.     }
  86.     for ( c = findfirst( filename, &tag, FA_DIREC | FA_RDONLY | FA_ARCH ) ;
  87.         !c ; c = findnext( &tag ) ) {
  88.         if ( kbhit() )
  89.             if ( getch() == 27 )
  90.                 halt = 1 ;
  91.         if ( halt )
  92.             break ;
  93.         puts( locatedfile( tag.ff_name ) ) ;
  94.         applyCommand( tag.ff_name ) ;
  95.     }
  96.  
  97.     chdir( ".." ) ;
  98. }
  99.  
  100. main( int argc, char *argv[] ) {
  101.  
  102.     if ( argc > 1 )
  103.         loc = argv[1] ;
  104.     if ( argc > 2 )
  105.         applyComd = argv[2] ;
  106.     getcwd( curDir, MAXPAT ) ;
  107.  
  108.     if ( attachTarget( loc ) )
  109.         recusiveSearch() ;
  110.  
  111.     setdisk( curDir[0] - 'A' ) ;
  112.     chdir( curDir ) ;
  113. }